import "./public-path";
import React from "react";
import ReactDOM from "react-dom";
import { createStore } from "redux";
import { Provider } from "react-redux";
import reducer from "./reducers/index";
import { setGlobalState } from "./actions";
import "@/locales/i18n.js";
import "./index.css";
import App from "./App";
import "antd/dist/antd.css/";
import "@/style/reset.css";

const store = createStore(
  reducer,
  window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
function render(props) {
  const { container } = props;
  ReactDOM.render(
    <Provider store={store}>
      <App />
    </Provider>,
    container
      ? container.querySelector("#root")
      : document.querySelector("#root")
  );
}

if (!window.__POWERED_BY_QIANKUN__) {
  render({});
}

export async function bootstrap() {
  console.log("[react16] react app bootstraped");
}

export async function mount(props) {
  console.log("[react16] props from main framework", props);
  const state = {
    isLogin: false,
    drawReactMain: false,
    modalReactMicro: false,
  };
  props.onGlobalStateChange((state, prev) => {
    // state: 变更后的状态; prev 变更前的状态
    // console.log(state, prev, "?SF@#RDGADT#$T@");
    store.dispatch(setGlobalState(state));
  });
  React.$setGlobalState = props.setGlobalState;
  React.$setGlobalState(state);
  render(props);
}

export async function unmount(props) {
  const { container } = props;
  ReactDOM.unmountComponentAtNode(
    container
      ? container.querySelector("#root")
      : document.querySelector("#root")
  );
}

window._AMapSecurityConfig = {
  securityJsCode: "198c0b0fe6b2f08420d1d4a10b0c61dc",
};
